<!DOCTYPE html>
<html lang="en">
<head>
    <title>权限管理</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="Cache-Control" content="no-store, must-revalidate">
    <meta http-equiv="expires" content="Wed, 26 Feb 1997 08:21:57 GMT">
    <meta http-equiv="expires" content="-1">
    <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"
          name="viewport">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="shortcut icon" href="/dist/img/icon.png" type="image/x-icon">
    <!-- Bootstrap 3.3.6 -->
    <link rel="stylesheet" href="/dist/plugins/bootstrap/css/bootstrap.min.css">
    <!--字体-->
    <link rel="stylesheet" href="/dist/css/font-awesome.min.css">
    <!--图标-->
    <link rel="stylesheet" href="/dist/css/ionicons.min.css">
    <!--插件样式-->
    <link href="/dist/plugins/ztree/css/zTreeStyle/zTreeStyle.css" rel="stylesheet"/>
    <!--OA主样式-->
    <link rel="stylesheet" href="/dist/css/OA.min.css?t=86e2297f">
    <link rel="stylesheet" href="/dist/css/skins/OA-skins.min.css?t=222aea35">
    <style>
        .searchBox {
            position: absolute;
            left: 0;
            top: 0;
            width: 100%;
            height: 25px;
            padding: 0 5px;
        }
    </style>
</head>
<body>
<section class="content" id="app">
    <div class="row">
        <div class="col-md-4" style="position: relative">
            <div class="searchBox">
                <input type="text" placeholder="搜索岗位名称..." class="form-control input-sm search-text"
                       style="width: 80%;float:left;" @keypress>
                <input type="button" class="btn btn-default btn-xs search-button oaBtn"
                       style="min-width:75px;float:left;height: 25px;width: 20%;" value="搜索">
            </div>
            <div class="box-body no-padding box-border iframeH4"
                 style="overflow: auto;background: #f7f8fa;margin-top: 25px;">
                <ul class="ztree" id="ulRoleName"></ul>
            </div>
        </div>
        <div class="col-md-4">
            <div class="box-body no-padding box-border iframeH2"
                 style="overflow: auto;background: #f7f8fa;">
                <ul class="ztree" id="ulRight"></ul>
            </div>
        </div>
        <div class="col-md-4">
            <div class="box-body no-padding box-border iframeH2"
                 style="overflow: auto;background: #f7f8fa;">
                <ul class="ztree" id="ulTemplate"></ul>
                <ul class="ztree" id="ulReportTemplate"></ul>
            </div>
        </div>
    </div>
    <div class="row text-center">
        <input type="button" value="保存" class="btn oaBtn btn-sm" id="btnSave"
               style="margin:10px 0;"/>
    </div>
    <span id="editInfo" style="color: #999999;position: absolute;right: 10px;bottom: 10px;"></span>
</section>
<div id="menu" style="width: 120px;">
    <div id="recover">
        恢复
    </div>
    <div id="setToFront0">
        设置为后台岗位
    </div>
    <div id="setToFront1">
        设置为一线岗位
    </div>
</div>
<!--JQ-->
<script src="https://file.ggxqce.com/web/jquery-2.2.3.min.js"></script>
<!--插件JS-->
<script src="https://file.ggxqce.com/web/bootstrap.min.js"></script>
<!--框架已定义 JS-->
<script src="/dist/js/app_iframe.js?t=8e743a31"></script>
<!--oa自定义  JS-->
<script src="/dist/js/oa.js?t=54c6b89c"></script>

<script src="https://file.ggxqce.com/web/vue.min.js"></script>
<!--ztree组件所需js-->
<script type="text/javascript" src="/dist/plugins/ztree/js/jquery.ztree.all-3.5.min.js"></script>
<script type="text/javascript" src="/dist/plugins/ztree/js/jquery.ztree.exhide-3.5.js"></script>
<script type="text/javascript" src="/dist/plugins/easyui/plugins/jquery.menu.js"></script>
<script type="text/javascript" src="/dist/plugins/easyui/plugins/jquery.parser.js"></script>

<script type="text/javascript">
    $(function () {
        $("#menu").menu({
            hideOnUnhover: false,
            onClick: function (item) {
                var node = zTreeRoleName.getSelectedNodes()[0];
                switch (item.id) {
                    case "recover":
                        node.delFlag = 0;
                        break;
                    case "setToFront0":
                        node.frontFlag = 0;
                        break;
                    case "setToFront1":
                        node.frontFlag = 1;
                        break;
                    default:
                        break;
                }
                $.wyui.postMethod(urlConfig.system.roleName.save, node, function (data) {
                    zTreeRoleName.updateNode(node, true);
                    zTreeRoleName.cancelSelectedNode(node);
                });
            }
        });
    });
</script>
<script type="text/javascript">
    var zTreeRoleName = null;
    var zTreeRight = null;
    var zTreeTemplate = null;
    //现有的权限数组
    var currentClickId = ''
    var selectedAllRightIds = []
    var selectedAllRightNames = []
    var zTreeRoleName_callback = {
        fontCss: function (treeId, treeNode) {
            return treeNode.delFlag ? {
                color: "#cccccc",
                padding: "0 50px 0 0",
                background: "url(/statics/img/stop.png)no-repeat right center"
            } : {color: "black", padding: "0", background: ""};
        },
        showIcon: function (treeId, treeNode) {
            return treeNode.type == 'root' || treeNode.frontFlag;
        },
        showRemoveBtn: function (treeId, treeNode) {
            return treeNode.type == "roleName" && treeNode.delFlag == 0;
        },
        showRenameBtn: function (treeId, treeNode) {
            return treeNode.type == "roleName" && treeNode.delFlag == 0;
        },
        addHoverDom: function (treeId, treeNode) {
            if (treeNode.type != "root" || treeNode.delFlag) return;
            var sObj = $("#" + treeNode.tId + "_span");
            if (treeNode.editNameFlag || $("#addBtn_" + treeNode.tId).length > 0) return;
            var addStr = "<span class='button add' id='addBtn_" + treeNode.tId
                + "' title='添加职位' onfocus='this.blur();'></span>";
            sObj.after(addStr);
            var btn = $("#addBtn_" + treeNode.tId);
            if (btn) btn.bind("click", function () {
                $.wyui.postMethod(urlConfig.system.roleName.save, {
                    id: '',
                    name: '新建岗位',
                    delFlag: 0,
                    frontFlag: 0,
                    sortNo: 0
                }, function (data) {
                    var newNode = zTreeRoleName.addNodes(treeNode, {
                        id: data.id,
                        name: '新建岗位',
                        delFlag: 0,
                        frontFlag: 0,
                        pId: treeNode.id,
                        type: 'roleName'
                    }, true);
                    zTreeRoleName.editName(newNode[0]);
                });
                return false;
            });
        },
        removeHoverDom: function (treeId, treeNode) {
            $("#addBtn_" + treeNode.tId).unbind().remove();
        },
        onClick: function (event, treeId, treeNode) {
            console.log(treeNode)
            getLastEdit()
            currentClickId = $(event.target).attr("id")
            // currentClickId = event
            var checkNodes = zTreeRight.getCheckedNodes("true")
            for (var i = 0, l = checkNodes.length; i < l; i++) {
                zTreeRight.checkNode(checkNodes[i], false, true);
            }
            //清除上一个选中状态
            var reportTemplateNode = zTreeReportTemplate.getCheckedNodes(true)[0];
            if (reportTemplateNode) {
                zTreeReportTemplate.checkNode(reportTemplateNode, false, true);
            }
            if (treeNode.type == "roleName" && treeNode.delFlag == 0) {
                if (treeNode.templateId) {
                    var nodes = zTreeTemplate.getNodesByParam("id", treeNode.templateId);
                    if (nodes.length > 0) {
                        zTreeTemplate.checkNode(nodes[0], true, true, true);
                    } else {
                        var nodes = zTreeTemplate.getNodesByParam("defaultFlag", 1);
                        if (nodes.length > 0) {
                            zTreeTemplate.checkNode(nodes[0], true, true, true);
                        }
                    }
                } else {
                    var nodes = zTreeTemplate.getNodesByParam("defaultFlag", 1);
                    if (nodes.length > 0) {
                        zTreeTemplate.checkNode(nodes[0], true, false, false);
                    }
                }
                if (treeNode.reportTemplateId) {
                    var nodes = zTreeReportTemplate.getNodesByParam("id", treeNode.reportTemplateId);
                    if (nodes.length > 0) {
                        zTreeReportTemplate.checkNode(nodes[0], true, true, true);
                    } else {
                        var nodes = zTreeReportTemplate.getNodesByParam("defaultFlag", 1);
                        if (nodes.length > 0) {
                            zTreeReportTemplate.checkNode(nodes[0], true, true, true);
                        }
                    }
                } else {
                    var nodes = zTreeReportTemplate.getNodesByParam("defaultFlag", 1);
                    console.log(nodes);
                    if (nodes.length > 0) {
                        zTreeReportTemplate.checkNode(nodes[0], true, false, false);
                    }
                }
                var selectedRightIds = []
                var selectedRightNames = []
                $.wyui.postMethod(urlConfig.system.roleName2Right.getByRoleNameId, {roleNameId: treeNode.id}, function (list) {
                    var rightIds = new Array();
                    for (var i = 0; i < list.length; i++) {
                        rightIds.push(list[i].rightId);
                    }
                    var nodes = zTreeRight.getNodesByFilter(function (node) {
                        return $.inArray(node.id, rightIds) > -1;
                    });
                    for (var i = 0; i < nodes.length; i++) {
                        zTreeRight.checkNode(nodes[i], true, false, false);
                        selectedRightIds.push(nodes[i].id)
                        selectedRightNames.push(nodes[i].name)
                    }
                }, true);
                var selectedRightOpIds = []
                var selectedRightOpNames = []
                $.wyui.postMethod(urlConfig.system.roleName2RightOp.getByRoleNameId, {roleNameId: treeNode.id}, function (list) {
                    var rightIds = new Array();
                    for (var i = 0; i < list.length; i++) {
                        rightIds.push(list[i].rightOpId);
                    }
                    var nodes = zTreeRight.getNodesByFilter(function (node) {
                        return $.inArray(node.id, rightIds) > -1;
                    });

                    for (var i = 0; i < nodes.length; i++) {
                        zTreeRight.checkNode(nodes[i], true, false, false);
                        selectedRightOpIds.push(nodes[i].id)
                        selectedRightOpNames.push(nodes[i].name)
                    }
                    selectedAllRightIds = selectedRightIds.concat(selectedRightOpIds)
                    selectedAllRightNames = selectedRightNames.concat(selectedRightOpNames)
                    //console.log("权限勾选了"+selectedRightIds.length+"个 : "+selectedRightNames)
                });
            }
        },
        onRightClick: function (event, treeId, treeNode) {
            if (!treeNode) return;
            zTreeRoleName.selectNode(treeNode);
            $("#menu").children().hide();
            switch (treeNode.type) {
                case "roleName":
                    if (treeNode.delFlag) {
                        $("#recover").show();
                    } else {
                        if (treeNode.frontFlag) {
                            $("#setToFront0").show();
                        } else {
                            $("#setToFront1").show();
                        }
                    }
                default:
                    break;

            }
            $('#menu').menu('show', {
                left: event.clientX,
                top: event.clientY
            });
        },
        beforeDrop: function (treeId, treeNodes, targetNode, moveType) {
            console.log(treeNodes)
            if (moveType == "inner") {
                return false;
            }
        },
        onDrop: function (event, treeId, treeNodes, targetNode, moveType, isCopy) {
            console.log(treeNodes);
            var node = treeNodes[0].getParentNode();
            console.log(node)
            var newNodes = new Array()
            for (var i = 0; i < node.children.length; i++) {
                var data = node.children[i]
                newNodes.push(data.id)
            }
            var obj = {roleNameIds: newNodes.join(",")}
            $.wyui.postMethod(urlConfig.system.roleName.sort, obj, function (data) {
                console.log(data)
            });

        },
        beforeEditName: function (treeId, treeNode) {
            zTreeRoleName.editName(treeNode);
            return false;
        },
        beforeRemove: function (treeId, treeNode) {
            if (confirm("确认删除吗？")) {
                treeNode.delFlag = 1;
                $.wyui.postMethod(urlConfig.system.roleName.save, treeNode, function (data) {
                    console.log(treeNode);
                    zTreeRoleName.updateNode(treeNode, true);
                    zTreeRoleName.cancelSelectedNode(treeNode);
                });
            }
            return false;
        },
        beforeRename: function (treeId, treeNode, newName, isCancel) {
            if (newName.length == 0) {
                setTimeout(function () {
                    zTreeRoleName.cancelEditName();
                    alert("不能为空");
                }, 0);
            } else {
                treeNode.name = newName;
                $.wyui.postMethod(urlConfig.system.roleName.save, treeNode, function () {
                    zTreeRoleName.cancelEditName(newName);
                    zTreeRoleName.cancelSelectedNode(treeNode);
                });
            }
            return false;
        },
        onRename: function (event, treeId, treeNode, isCancel) {

        }
    };
    var zTreeRoleName_setting = {
        view: {
            fontCss: zTreeRoleName_callback.fontCss,
            addHoverDom: zTreeRoleName_callback.addHoverDom,
            removeHoverDom: zTreeRoleName_callback.removeHoverDom,
            showIcon: zTreeRoleName_callback.showIcon
        },
        data: {
            simpleData: {
                enable: true
            },
            keep: {
                parent: true
            }
        },
        callback: {
            onClick: zTreeRoleName_callback.onClick,
            onRightClick: zTreeRoleName_callback.onRightClick,
            beforeDrag: zTreeRoleName_callback.beforeDrag,
            beforeDrop: zTreeRoleName_callback.beforeDrop,
            onDrop: zTreeRoleName_callback.onDrop,
            beforeEditName: zTreeRoleName_callback.beforeEditName,
            beforeRemove: zTreeRoleName_callback.beforeRemove,
            beforeRename: zTreeRoleName_callback.beforeRename,
            onRemove: zTreeRoleName_callback.onRemove,
            onRename: zTreeRoleName_callback.onRename
        },
        edit: {
            enable: true,
            editNameSelectAll: true,
            removeTitle: '删除',
            renameTitle: '修改',
            showRemoveBtn: zTreeRoleName_callback.showRemoveBtn,
            showRenameBtn: zTreeRoleName_callback.showRenameBtn
        }
    };
    var zTreeRight_setting = {
        view: {
            showIcon: function (treeId, treeNode) {
                return treeNode.type == "root" || treeNode.type == "rightFolder";
            }
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        check: {
            chkboxType: {"Y": "p", "N": "s"},
            enable: true
        },
        callback: {
            onClick: function (e) {
                e.stopPropagation();
                e.preventDefault();
                return false;
            }
        }
    };
    var zTreeTemplate_setting = {
        view: {
            showIcon: function (treeId, treeNode) {
                return treeNode.isParent;
            }
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        check: {
            enable: true,
            chkStyle: "radio"
        }
    };
    var zTreeReportTemplate_setting = {
        view: {
            showIcon: function (treeId, treeNode) {
                return treeNode.isParent;
            }
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        check: {
            enable: true,
            chkStyle: "radio"
        }
    };
    $(function () {
        getLastEdit()
        $.wyui.postMethod(urlConfig.system.roleName.getTreeNodes, {}, function (nodes) {
            zTreeRoleName = $.fn.zTree.init($("#ulRoleName"), zTreeRoleName_setting, nodes);
        });
        $.wyui.postMethod(urlConfig.system.right.getTreeNodes, {}, function (nodes) {
            for (var i = 0; i < nodes.length; i++) {
                if (nodes[i].type == "right") {
                    if (nodes[i].description) {
                        nodes[i].name = nodes[i].name + "(" + nodes[i].description + ")";
                    }
                }
            }
            zTreeRight = $.fn.zTree.init($("#ulRight"), zTreeRight_setting, nodes);
            var nodes = zTreeRight.getNodesByFilter(function (node) {
                if (node.name == "常用功能") {
                } else {
                    return node.defaultFlag;
                }
            });
            for (var i = 0; i < nodes.length; i++) {
                zTreeRight.checkNode(nodes[i], true, false);
                nodes[i].chkDisabled = true;
            }
        });
        $.wyui.postMethod(urlConfig.wreport.config.template.getTreeNodes, {}, function (nodes) {
            zTreeTemplate = $.fn.zTree.init($("#ulTemplate"), zTreeTemplate_setting, nodes);
        });
        $.wyui.postMethod(urlConfig.report.config.template.getTreeNodes, {}, function (nodes) {
            zTreeReportTemplate = $.fn.zTree.init($("#ulReportTemplate"), zTreeReportTemplate_setting, nodes);
        });
        $("#btnSave").on("click", function () {
            var roleNameNode = zTreeRoleName.getSelectedNodes()[0];
            if (!roleNameNode || roleNameNode.type != "roleName") {
                alert("请选择岗位");
                return;
            }
            var templateNode = zTreeTemplate.getCheckedNodes(true)[0];
            var reportTemplateNode = zTreeReportTemplate.getCheckedNodes(true)[0];
            roleNameNode.templateId = templateNode.id;
            if (reportTemplateNode) {
                roleNameNode.reportTemplateId = reportTemplateNode.id;
            }
            $.wyui.postMethod(urlConfig.system.roleName.save, roleNameNode, function (data) {
                var arrRightIds = new Array();
                var arrRightOpIds = new Array();
                var arrRightNames = new Array();
                var arrRightOpNames = new Array();

                var rightNodes = zTreeRight.getCheckedNodes(true);
                for (var i = 0; i < rightNodes.length; i++) {
                    if (rightNodes[i].id && rightNodes[i].id != "0") {
                        if (rightNodes[i].type == "right" || rightNodes[i].type == "rightFolder") {
                            arrRightIds.push(rightNodes[i].id);
                            arrRightNames.push(rightNodes[i].name);
                        } else {
                            arrRightOpIds.push(rightNodes[i].id);
                            arrRightOpNames.push(rightNodes[i].name);
                        }
                    }
                }

                $.wyui.postMethod(urlConfig.system.roleName2Right.config, {
                    roleNameId: roleNameNode.id,
                    rightIds: arrRightIds.join(",")
                }, function () {
                    //记录日志
                    var arrAllRightIds = arrRightIds.concat(arrRightOpIds)
                    var arrAllRightNames = arrRightNames.concat(arrRightOpNames)

                    //判断修改类型
                    var beforeIds = selectedAllRightIds.join(",")
                    var beforeNames = selectedAllRightNames.join(",")
                    var afterIds = arrAllRightIds.join(",")
                    var afterNames = arrAllRightNames.join(",")

                    if (beforeIds == afterIds) {
                        //未改动
                        return
                    }
                    var type = 1
                    var remark = ''
                    if (beforeIds.length < afterIds.length) {
                        //新增
                        type = 0
                        remark = selectedAllRightNames.concat(arrAllRightNames).filter(function (v) {
                            return selectedAllRightNames.indexOf(v) === -1 || arrAllRightNames.indexOf(v) === -1
                        }).join(",")
                    } else if (beforeIds.length == afterIds.length) {
                        //修改
                        type = 1
                        //取交集
                        var common = beforeNames.filter(function (v) {
                            return afterNames.indexOf(v) !== -1
                        })
                        var diff1 = selectedAllRightNames.concat(common).filter(function (v) {
                            return selectedAllRightNames.indexOf(v) === -1 || arrAllRightNames.indexOf(v) === -1
                        }).join(",")
                        var diff2 = arrAllRightNames.concat(common).filter(function (v) {
                            return selectedAllRightNames.indexOf(v) === -1 || arrAllRightNames.indexOf(v) === -1
                        }).join(",")
                        remark = diff1 + ",修改为:" + diff2
                    } else {
                        //删除
                        type = 2
                        remark = selectedAllRightNames.concat(arrAllRightNames).filter(function (v) {
                            return selectedAllRightNames.indexOf(v) === -1 || arrAllRightNames.indexOf(v) === -1
                        }).join(",")
                    }
                    $.wyui.postMethod(urlConfig.system.roleRightConfigLog.save, {
                        createUserId: getCurrentUser().id,
                        roleNameId: roleNameNode.id,
                        changeBeforeIds: selectedAllRightIds.join(","),
                        changeBeforeNames: selectedAllRightNames.join(","),
                        changeAfterIds: arrAllRightIds.join(","),
                        changeAfterNames: arrAllRightNames.join(","),
                        changeType: type,
                        remark: remark,
                        userId:'0',
                        rightType:'0'
                    }, function () {
                    });
                }, true);

                $.wyui.postMethod(urlConfig.system.roleName2RightOp.config, {
                    roleNameId: roleNameNode.id,
                    rightOpIds: arrRightOpIds.join(",")
                }, function () {
                    alert("保存成功");
                    //刷新
                    $("#" + currentClickId).click();
                });
            });
        });
        $(".search-button").on('click', function () {
            searchTree()
        });

        function searchTree() {
            var text = $(".search-text").val();
            var treeObj = $.fn.zTree.getZTreeObj("ulRoleName");
            if (text != "") {
                var allNode = treeObj.transformToArray(treeObj.getNodes());
                ;
                treeObj.hideNodes(allNode);
                nodeList = treeObj.getNodesByParamFuzzy("name", text);
                nodeList = treeObj.transformToArray(nodeList);
                for (var n in nodeList) {
//                    treeObj.selectNode(nodeList[n], true);
                    findParent(treeObj, nodeList[n]);
                }
                treeObj.showNodes(nodeList);

            } else {
                var allNode = treeObj.transformToArray(treeObj.getNodes());
                ;
                treeObj.showNodes(allNode);
                treeObj.cancelSelectedNode();
                treeObj.expandAll(false);
                for (var index in allNode) {
                    if (allNode[index].level < 2) {
                        treeObj.expandNode(allNode[index], true, false, false);
                    }
                }

            }
        }

        function findParent(treeObj, node) {
            treeObj.expandNode(node, true, false, false);
            var pNode = node.getParentNode();
            if (pNode != null) {
                nodeList.push(pNode);
                findParent(treeObj, pNode);
            }
        }
    });

    function getLastEdit() {
        //获取最后一次操作日志
        $.wyui.postMethod(urlConfig.system.roleRightConfigLog.getLogList, {
            requestPage: 1,
            pageSize: 1
        }, function (data) {
            if (data.records && data.records.length > 0) {
                var log = data.records[0]
                var type = ""
                if (log.changeType == 0) type = "权限: "
                if (log.changeType == 1) type = "将 "
                if (log.changeType == 2) type = "权限: "
                var info = "岗位: " + log.roleName + "\n操作: " + type + log.remark
                $("#editInfo").html("最后由 <b>" + log.createUserName + "</b> 编辑于 <b>" + log.createTime + "</b>&nbsp;&nbsp;<i class='fa fa-question-circle-o'></i>");
                $("#editInfo").attr("title", info)
            }
        });
    }
</script>
</body>
</html>